//安装 npm i express
//导入 express
const express = require('express');

//创建服务对象
const app = express();

//封装路由中间件函数
let checkVipMiddleware = (req, res, next) => {
  //判断
  if(req.query.vip !== '1'){
    return res.status(403).send('<h2>权限不足</h2>')
  }
  next();
}

//创建路由规则
app.get('/', (req, res) => {
  res.end('欢迎来到红浪漫~~');
});

app.get('/hello', (req, res) => {
  res.end('Hello Express');
});

//要求访问 /admin 时必须要添加 url query 参数  vip=1
app.get('/admin', checkVipMiddleware, (req, res) => {
  res.send('后台首页');
});

app.get('/setting',checkVipMiddleware, (req, res) => {
  res.send('后台设置')
});

//监听端口 启动服务
app.listen(80, () => {
  console.log('服务已经启动....');
})